package com.xlabs.client;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.xlabs.client.Elements.TextLink;

/**
 * The Questions class.
 * @author:a.akhavan.b
 */
public class Questions extends ScrollPanel {
    /**
     * video widget with which these questions associate.
     */
    private ChromelessPlayer video;

    /**
     * Conversation UI for questions.
     */
    private Conversation conversation;

    /**
     * Last question clicked.
     */
    private static int LastQuestionClicked = -1;

    /**
     * Initializes a new instance of the Questions class.
     * @param video video widget with which these questions associate.
     * @param conversation Conversation UI for questions.
     */
    public Questions(ChromelessPlayer video, Conversation conversation) {
        this.video = video;
        this.conversation = conversation;
    }

    /**
     * Displays returned comments from server.
     * @param questions input questions.
     */
    public void displayQuestions(CommentDTO[] questions) {
        Grid allQuestions = new Grid(questions.length + 1, 4);
        allQuestions.getColumnFormatter().setWidth(0, "100");
        allQuestions.getColumnFormatter().setWidth(1, "1000");
        allQuestions.setCellPadding(2);

        this.setWidget(allQuestions);

        allQuestions.setWidget(0, 0, new Label("Time"));
        allQuestions.setWidget(0, 1, new Label("Comment"));
        allQuestions.setWidget(0, 2, new Label("User"));
        allQuestions.setWidget(0, 3, new Label("Action"));
        allQuestions.getRowFormatter().addStyleName(0, "gridHeader");

        int index = 1;
        for (CommentDTO aComment : questions)
        {
            Label timeCell = new Label("" + aComment.getTime());
            timeCell.addClickListener(new CommentTimeListener(aComment.getTime()));
            timeCell.addStyleName("SimpleTextLink");
            allQuestions.setWidget(index, 0, timeCell);

            Label questionCell = new Label(aComment.getComment());
            questionCell.addStyleName("SimpleTextLink");
            questionCell.addClickListener(new QuestionListener(aComment.getEncodedKey(), this.conversation, index, allQuestions));
            allQuestions.setWidget(index, 1, questionCell);

            Label userCell = new Label(aComment.getCreatedBy());
            allQuestions.setWidget(index, 2, userCell);

            TextLink replyLink = new TextLink("Reply");
            replyLink.addClickListener(new ReplyListener(index, allQuestions, aComment.getEncodedKey()));
            allQuestions.setWidget(index, 3, replyLink);

            index = index + 1;
        }
    }

    /**
     * CommentListener class.
     */
    private class QuestionListener implements ClickListener {
        /**
         * parent's encoded key.
         */
        private String parentEncodedKey;

        /**
         * converssation widget to be updated.
         */
        private Conversation conversation;

        /**
         * Index of question cell on questionGrid.
         */
        private int index;

        /**
         * Question grid source.
         */
        private Grid questionGrid;

        /**
         * Constructor.
         * @param parentEncodedKey parent's encoded key.
         * @param conversation converssation widget to be updated.
         * @param index   Index of question cell on questionGrid.
         * @param questionGrid  Question grid source.
         */
        private QuestionListener(String parentEncodedKey, Conversation conversation, int index, Grid questionGrid) {

            this.parentEncodedKey = parentEncodedKey;
            this.conversation = conversation;
            this.index = index;
            this.questionGrid = questionGrid;
        }

        /**
         * onClick.
         * @param widget widget that got clicked on.
         */
        public void onClick(Widget widget) {
            this.conversation.showConversation(this.parentEncodedKey);
            
            if (Questions.LastQuestionClicked != -1)
            {
                this.questionGrid.getRowFormatter().removeStyleName(Questions.LastQuestionClicked, "ClickedQuestion");
            }
            
            this.questionGrid.getRowFormatter().addStyleName(index, "ClickedQuestion");
            
            Questions.LastQuestionClicked = index;
        }
    }

    /**
     * ReplyListener class.
     */
    private class ReplyListener implements ClickListener {

        /**
         * index of reply.
         */
        private int index;

        /**
         * Comments grid.
         */
        private Grid commentsGrid;

        /**
         * Question key.
         */
        private String questionKey;

        /**
         * Initializes an instance of ReplyListener.
         * @param index index of row clicked.
         * @param commentsGrid The comments grid.
         * @param questionKey key of the question.
         */
        public ReplyListener(int index, Grid commentsGrid, String questionKey) {
            this.index = index;
            this.commentsGrid = commentsGrid;
            this.questionKey = questionKey;
        }

        /**
         * Onclick.
         * @param widget widget.
         */
        public void onClick(Widget widget) {
            Label commentLAbel = (Label) this.commentsGrid.getWidget(index, 1);
            VerticalPanel replyBox = new VerticalPanel();
            replyBox.add(commentLAbel);

            final TextArea replyArea = new TextArea();
            replyArea.setSize("300px", "100px");
            replyBox.add(replyArea);

            Button submitButton = new Button("Submit", new ClickListener() {
                public void onClick(Widget widget) {

                    CommentingServiceAsync myService = (CommentingServiceAsync) GWT.create(CommentingService.class);

                    myService.postReply(replyArea.getText(), ReplyListener.this.questionKey, new AsyncCallback() {

                        public void onFailure(Throwable throwable) {
                            replyArea.setText("Failed to post Reply : " + replyArea.getText() + " Because of " + throwable);
                        }

                        public void onSuccess(Object o) {
                            replyArea.setText("Posted Reply : " + replyArea.getText());
                            replyArea.setReadOnly(true);
                            displayQuestions((CommentDTO[]) o);
                        }
                    });
                }
            });

            replyBox.add(submitButton);

            this.commentsGrid.setWidget(index, 1, replyBox);
        }
    }

    /**
     * CommentListener class.
     */
    private class CommentTimeListener implements ClickListener {

        /**
         * Time of the comment.
         */
        private double time;

        public CommentTimeListener(double time) {
            this.time = time;
        }

        /**
         * onClick.
         * @param widget widget.
         */
        public void onClick(Widget widget) {
            video.seekTime(time);
        }
    }

}
